package com.thoughtworks.opensource.yui.dd.client.triads.incomingnumbers;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.thoughtworks.opensource.yui.dd.client.DdService;
import com.thoughtworks.opensource.yui.dd.client.EventPublisher;
import com.thoughtworks.opensource.yui.dd.client.domain.MyNumber;
import com.thoughtworks.opensource.yui.dd.client.events.Event;
import com.thoughtworks.opensource.yui.dd.client.events.EventType;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * Author: C. K. Sidu Ponnappa
 * Date: Nov 22, 2006
 * Time: 8:34:49 PM
 * ThoughtWorks India Pvt. Ltd.
 */
public class IncomingNumbersModel extends EventPublisher {
    private ArrayList myNumbers;

    public IncomingNumbersModel() {
        super();
        myNumbers = new ArrayList();
        getNumbersAgain();
    }

    public void getNumbersAgain() {
        DdService.App.getInstance().getNumbers(new AsyncCallback() {
            public void onFailure(Throwable caught) {
            }

            public void onSuccess(Object result) {
                MyNumber[] incomingNumbers = (MyNumber[]) result;
                for (int i = 0; i < incomingNumbers.length; i++) {
                    myNumbers.add(incomingNumbers[i]);
                }
                notifyListeners(new Event(EventType.MODEL_CHANGED, this, this));
            }
        });
    }

    public int getCount() {
        return myNumbers.size();
    }

    public MyNumber get(int index) {
        return (MyNumber) myNumbers.get(index);
    }
}
